<?php

namespace App\models\forum;

use App\models\user\User;
use DB;
use Illuminate\Database\Eloquent\Model;

/**
 * App\models\forum\TopicVote
 *
 * @property int $uid
 * @property int $topic_id
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\TopicVote whereUid($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\TopicVote whereTopicId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\TopicVote whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\models\forum\TopicVote whereUpdatedAt($value)
 * @mixin \Eloquent
 * @property-read \App\models\user\User $user
 */
class TopicVote extends Model
{
    public function deleteMe(){
        return DB::delete('delete FROM topic_votes where uid = ? and topic_id = ?', [$this->uid, $this->topic_id]);
    }

    public function user(){
        return $this->belongsTo(User::class, 'uid', 'id');
    }

    public static function getVotes($topic_id){
        $votes = self::whereTopicId($topic_id)->get();
        $voteUsers = array();
        foreach ($votes as $vote) {
            $user = $vote->user;
            array_push($voteUsers, ['uid'=>$user->id, 'aratar'=> $user->profile]);
        }
        return $voteUsers;
    }
}
